#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Modulos 
import sys, gtk, pygame, utils, objetos
from pygame.locals import *

# Constantes

# Clases
# ---------------------------------------------------------------------

class ObjetoGraficoTexto(pygame.sprite.Sprite):
	def __init__(self, texto, posInicialx, posInicialy, posCorrectax, posCorrectay ):
		fuente = pygame.font.Font("fonts/justadream_solid.ttf", int(30*utils.RES))
		self.image = pygame.font.Font.render(fuente, texto, 1, (50,50,255))
		self.rect = self.image.get_rect()

		self.rect.centerx = posInicialx
		self.rect.centery = posInicialy

		self.valor = texto
		self.posInicialx = posInicialx
		self.posInicialy = posInicialy

		self.posCorrectax = posCorrectax
		self.posCorrectay = posCorrectay

		self.drag = False
		self.correcta = False
		self.sonido_correcto = pygame.mixer.Sound("sounds/click.wav")
		self.sonido_fallo = pygame.mixer.Sound("sounds/fallo.wav")


	def colocarPosicionInicial(self):
		self.rect.centerx = self.posInicialx
		self.rect.centery = self.posInicialy
		self.correcta = False			

	def verificarPosCorrecta(self):
		if abs(self.rect.centerx - self.posCorrectax) < 12 and abs(self.rect.centery - self.posCorrectay) < 15 :
			self.rect.centerx = self.posCorrectax
			self.rect.centery = self.posCorrectay
			self.correcta = True
			self.sonido_correcto.play()
			return True
		else:
			return False


class Textbox:
	def __init__(self,posx,posy, xboton, yboton):
		self.font = pygame.font.Font("fonts/TRSL____.ttf", int(18*utils.RES))
		self.rect = pygame.Rect([0, 0, 220*utils.RES, 22*utils.RES])
		offset = (3, 3)
		self.rect.centerx = posx
		self.rect.centery = posy
		self.rect.left += offset[0]
		self.rect.top  += offset[1]
		self.message = ""
		self.selected = False
		self.puntos,self.puntos_rect = utils.texto_normal('.............................................................................', (posx+80)*utils.RES,(posy+10)*utils.RES)
		self.maxlenght = 50
		self.sonido = pygame.mixer.Sound("sounds/letra.ogg")

        	self.pixel = utils.load_image('images/pixel.png', False)
        	self.pixel_rect = self.pixel.get_rect()
        	self.pixel_rect.centerx = xboton*utils.RES
	        self.pixel_rect.centery = yboton*utils.RES

        	self.pixel2 = utils.load_image('images/pixel2.png', False)
	        self.pixel2_rect = self.pixel.get_rect()
        	self.pixel2_rect.centerx = (xboton - 5)*utils.RES
	        self.pixel2_rect.centery = (yboton - 5)*utils.RES

		
	def display(self,screen):
		screen.blit(self.puntos,self.puntos_rect)
		if len(self.message) != 0:
			screen.blit(self.font.render(self.message, 1, (0,0,0)), self.rect.topleft)	


# ---------------------------------------------------------------------

def dibujar(screen,cursor_picture, clock):
    salir = 0
    background_image = utils.load_image('images/fondo2.png')

    #boton de la flecha para continuar
    btn_adelante = objetos.BotonAdelante()
    btn_atras = objetos.BotonAtras()

    #texto superior derecho que indica el numero de clase
    clase2, clase2_rect = utils.texto_clase("Clase 2", 750*utils.RES, 15*utils.RES)

    x1 = 370
    y1 = 50
    linea1,linea1_rect = utils.textotlb('Y volviendo al mapa de nuestro amigo navegante, Bernardo Lasaguas.... ', 		  x1*utils.RES,  y1*utils.RES, 
				20, color=(0, 0, 0))
    linea3,linea3_rect = utils.textotls('¡Nos llegaron las pistas para descifrar el mensaje!', 	      250*utils.RES,(y1+(2*25))*utils.RES,
				20, color=(0, 0, 0))
    linea4,linea4_rect = utils.textotls('¿Te animás a resolverlo? Escribí aquí el mensaje completo:', 250*utils.RES,(y1+(3*25))*utils.RES,
				20, color=(0, 0, 0))
           
    pista = utils.load_image('images/clase2_pista.png', False)
    pista_rect = pista.get_rect()
    pista_rect.centerx = 600*utils.RES
    pista_rect.centery = 110*utils.RES

    #clase2_mail = utils.load_image('images/clase2_mail.png', False)

    derecha = 220
    textboxes = (Textbox(derecha*utils.RES,180*utils.RES,  90*utils.RES,180*utils.RES), 
		 Textbox(derecha*utils.RES,210*utils.RES,  90*utils.RES,210*utils.RES),
		 Textbox(derecha*utils.RES,240*utils.RES,  90*utils.RES,240*utils.RES),
		 Textbox(derecha*utils.RES,270*utils.RES,  90*utils.RES,270*utils.RES),
		 Textbox(derecha*utils.RES,300*utils.RES,  90*utils.RES,300*utils.RES),
		 Textbox(derecha*utils.RES,330*utils.RES,  90*utils.RES,330*utils.RES),
		 Textbox(derecha*utils.RES,360*utils.RES,  90*utils.RES,360*utils.RES)
		)
    px = 0
    while salir == 0:
    
    	# Pump GTK messages.
		while gtk.events_pending():
			gtk.main_iteration()
			
		for eventos in pygame.event.get():
			if eventos.type == QUIT:
				sys.exit(0)
			elif eventos.type == pygame.VIDEORESIZE:
			   pygame.display.set_mode(eventos.size, pygame.RESIZABLE)	
			elif eventos.type == MOUSEBUTTONDOWN:
				x,y = pygame.mouse.get_pos()
				if btn_adelante.rect.collidepoint(x, y) :
					salir = -1
					btn_adelante.sonido.play()	
				elif btn_atras.rect.collidepoint(x, y):
					salir = 12
					btn_atras.sonido.play()	
				for textbox in textboxes:
					if textbox.pixel_rect.collidepoint(x,y):
						textbox.selected = True
						textbox.sonido.play()
					else:
						textbox.selected = False	

			elif eventos.type == KEYDOWN:		
				if eventos.key == K_BACKSPACE:
					for textbox in textboxes:
						if textbox.selected == True: 
							textbox.message = textbox.message[0:-1]
				elif eventos.key == K_RETURN:
					break
				else:
					for textbox in textboxes:
						if textbox.selected == True: 
							textbox.sonido.play()
							if  len(textbox.message) <= textbox.maxlenght:
								textbox.message += eventos.unicode.encode("ascii")			

		# Realiza la animacion del texto	
		screen.blit(background_image, (0, 0))
		screen.blit(clase2, clase2_rect)

		screen.blit(linea1,linea1_rect)
		screen.blit(linea3,linea3_rect)
		screen.blit(linea4,linea4_rect)

		screen.blit(pista,pista_rect)

		px = px + 1
		for textbox in textboxes:
			textbox.display(screen)
			if textbox.selected == False:
				screen.blit(textbox.pixel, textbox.pixel_rect)
			else:
				if px <= 20 :
					screen.blit(textbox.pixel, textbox.pixel_rect)
				elif px <= 40 :
					screen.blit(textbox.pixel2, textbox.pixel2_rect)
				else:
					px = 0

			
		#screen.blit(clase2_mail, (400,180))

		screen.blit(btn_adelante.image, btn_adelante.rect)
		screen.blit(btn_atras.image, btn_atras.rect)
		screen.blit(cursor_picture, pygame.mouse.get_pos())
		pygame.display.flip()

		# Try to stay at 30 FPS
		clock.tick(30)
    return salir


# This function is called when the game is run directly from the command line:
# ./TestGame.py 
def main():
	
	pygame.init()
	screen = pygame.display.set_mode((int(800*utils.RES), int(520*utils.RES)), pygame.RESIZABLE) #pygame.display.get_surface() #

	# Fake a Sugar cursor for the pyGame canvas area
	pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
	cursor_picture = pygame.image.load('images/standardcursor.png')
	cursor_picture.convert_alpha() 
	clock = pygame.time.Clock()
	dibujar(screen,cursor_picture,clock)
 
if __name__ == '__main__':
   main()
